package com.longxiao.manager.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.longxiao.exception.LXException;
import com.longxiao.service.TeacherLectureService;
import com.longxiao.service.UserService;
import com.longxiao.vo.DwzResult;
import com.longxiao.vo.PageVo;
import com.longxiao.vo.TchUserVo;

@Controller
@RequestMapping(value = "teacher")
public class TeacherController extends BaseController {
	@Resource
	private TeacherLectureService teacherLectureService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping(value = "list")
	public ModelAndView toListPage(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("teacher/list");
	}
	
	@RequestMapping(value = "toPayLecture")
	public ModelAndView toPayLecture(String tchId,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("teacher/payLecture");
		try {
			mav.addObject("tchUserVo", userService.getUserInfo(tchId));
		} catch (LXException e) {
			logger.error("获取用户信息失败：" + e.getMessage(),e);
		}
		return mav;
	}
	
	@RequestMapping(value = "getList",method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getList(
			@RequestParam(value = "mobile",required = false)String mobile,
			@RequestParam(value = "email",required = false)String email,
			@RequestParam(value = "nickname",required = false)String nickname,
			@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",required = false,defaultValue = "20")int pageSize,
			HttpServletRequest request) {
		TchUserVo tchUserVo = new TchUserVo();
		tchUserVo.setEmail(email);
		tchUserVo.setMobile(mobile);
		tchUserVo.setNickname(nickname);
		logger.info("获取老师信息：{}",tchUserVo);
		int count = teacherLectureService.countTchUserVoList(tchUserVo);
		PageVo<TchUserVo> pageInfo = new PageVo<TchUserVo>(pageNum, count);
		List<TchUserVo> userVoList = teacherLectureService.getTchUserVoList(tchUserVo, pageInfo.getOffset(), pageInfo.getRows());
		
		ModelAndView mav = new ModelAndView("teacher/list");
		mav.addObject("totalCount", count);
		mav.addObject("tchList", userVoList);
		mav.addObject("currentPage", pageNum);
		mav.addObject("tchUserVo", tchUserVo);
		
		initParam(request,mav);
		return mav;
	}
	
	@RequestMapping(value = "payLecture",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> payLecture(@RequestParam(value = "tchId",required = false)String tchId,
			@RequestParam(value = "payLectures",required = false)Integer payLectures,HttpServletRequest request) {
		logger.info("为老师支付课程：{}，{}",tchId,payLectures);
		try {
			boolean res = teacherLectureService.payLecture(tchId, payLectures);
			if(res) {
				return DwzResult.success();
			}
		} catch (Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
		return DwzResult.fail("系统异常");
	}
}
